
package ParseURL;

import java.io.*;
import java.net.*;
import java.util.Locale;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 *
 * @author Chris
 */
public class GetDomain {
	
	private static WritableCellFormat timesBoldUnderline;
	private static WritableCellFormat times;
	private static String inputFile;
	private static WritableSheet excelSheet;
	public static WritableWorkbook workbook;
	


/*public void write() throws IOException, WriteException {
	File file = new File(inputFile);
	WorkbookSettings wbSettings = new WorkbookSettings();

	wbSettings.setLocale(new Locale("en", "EN"));

	this.workbook = Workbook.createWorkbook(file, wbSettings);
	this.workbook.createSheet("Report",0);
	this.excelSheet = workbook.getSheet(0);
	createLabel(this.excelSheet);
}*/
	



    public static void main(String[] args) throws Exception {
    	
    	 if (args.length != 1) {
             System.err.println("Usage:  GetDomain Filename");
             System.exit(1);
         }
    	 
    	// setOutputFile("C:/Aparna/test.xls");
    	 
    	int cnt =0; 
    	File file = new File("C:/Aparna/test2.xls");
    	WorkbookSettings wbSettings = new WorkbookSettings();

    	wbSettings.setLocale(new Locale("en", "EN"));

    	workbook = Workbook.createWorkbook(file, wbSettings);
    	workbook.createSheet("Report",0);
    	excelSheet = workbook.getSheet(0);
    	createLabel(excelSheet);
    	
 		System.out.println("Please check the result file under c:/Aparna/test.xls ");
    	 
    	 
      int cntCom = 0;
      int cntOrg = 0;
      int cntEdu = 0;
      int cntGov = 0;
      int cntOther = 0;
      int cntCountry = 0;
      String category = new String();
      
      
    	
      try{
		  // Open the file that is the first 
		  // command line parameter
		BufferedReader br = new BufferedReader(new FileReader(args[0]));
		  String strLine;
		  String hostName;
		  //String domainName;
		  //Read File Line By Line
		  while ((strLine = br.readLine()) != null)   {
	    		  // Print the content on the console
			  //System.out.println(strLine);
	    		  URL aUrl =  new URL(strLine);
	    		  hostName = aUrl.getHost();
	    		 String revHostName = new StringBuffer(hostName).reverse().toString();
	    		  String delims = "[.]+";
	    			String[] tokens = revHostName.split(delims);
	    			// System.out.println(tokens[0]);
	    			if (tokens[0].equals(new String("moc")))
	    			{
	    				cntCom++;
                if (tokens[1].equals(new String("koobecaf")))
                   {
				category = "Facebook Page";
				
				}
                else if (tokens[1].equals(new String("ecapsym")))
                   {
				category = "MySpace Page";
				
				}
else if (tokens[1].equals(new String("rettiwt")))
                   {
				category = "Twitter Page";
				
				}
else if (tokens[1].equals(new String("nideknil")))
                   {
				category = "Linked In Page";
				
				}
else if (tokens[1].equals(new String("5ih")))
                   {
				category = "Hi5 Page";
				
				}

else if (tokens[1].equals(new String("tukor")))
                   {
				category = "Orkut Page";
			
				}

else if (tokens[1].equals(new String("ggid")))
                   {
				category = "Digg";
				
				
				}

else if (tokens[1].equals(new String("aidepikiw")))
                   {
				category = "Wikipedia Page";
				
				}

else if (tokens[1].equals(new String("sserpdrow")))
                   {
				category = "Wordpress Page";
				
				}
else if (tokens[1].equals(new String("golatacgolb")))
                   {
				category = "Blog Catalog Page";
				
				}
else if (tokens[1].equals(new String("segapetihw")))
                   {
				category = "WhitePages Page";
				
				}
else if (tokens[1].equals(new String("segapwolley")))
                   {
				category = "Yellowpages Page";
				
				}


else 
category = "In Domain .com";
               

	    			}
	    			else if (tokens[0].equals(new String("gro")))
	    			{
	    				cntOrg++;
                        category = "Organization";
                        //System.out.println(strLine);
	    			}
	    			else if (tokens[0].equals(new String("ude")))
	    			{
	    				cntEdu++;
					category = "Education";
					
	    			}
	    			else if (tokens[0].equals(new String("vog")))
	    			{
	    				cntGov++;
					category = "Government";
					 System.out.println(strLine);

	    			}
	    			else if (tokens[0].length() == 2) 
	    			{
	    				String revToken = new StringBuffer(tokens[0]).reverse().toString();
	    				//System.out.println(revToken);
	    				cntCountry++;
					category = "Government" + revToken;
				

	    			}
	    			else	
	    			{
	    			cntOther++	;
                       category = "Others";
                      
            
	    			}
	    		cnt++;
	    		
		//test.writeData(strLine, category, cnt);
	    		createContent(strLine, category, cnt);    
	    		//workbook.write();
			
		  		} //while ends	
		  //System.out.println (strLine);
		  
		  //Close the input stream
		  //test.closeWB();
		  //in.close();
		
		  
		}catch (Exception e){//Catch exception if any
		  System.err.println("Error " + e.getMessage() );
			//e.printStackTrace();
		  }

		
		  workbook.write();
			System.out.println("Written to excel");
			//this.workbook.close();
			System.out.println("Closed excel");
    		   		
			System.out.println("Com sites = " + cntCom);
			System.out.println("Org sites = " + cntOrg);
			System.out.println("Edu sites = " + cntEdu);
			System.out.println("Gov sites = " + cntGov);
			System.out.println("Other sites = " + cntOther);
			System.out.println("Country sites = " + cntCountry);
    		
    		
    		
    		}//main ends
    
    
   public static void createLabel(WritableSheet sheet)
	throws WriteException {
// Lets create a times font
WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
// Define the cell format
times = new WritableCellFormat(times10pt);
// Lets automatically wrap the cells
times.setWrap(true);

// Create create a bold font with unterlines
WritableFont times10ptBoldUnderline = new WritableFont(
		WritableFont.TIMES, 10, WritableFont.BOLD, false,
		UnderlineStyle.SINGLE);
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
// Lets automatically wrap the cells
timesBoldUnderline.setWrap(true);

CellView cv = new CellView();
cv.setFormat(times);
cv.setFormat(timesBoldUnderline);
cv.setAutosize(true);

// Write a few headers
//addCaption(sheet, 0, 0, "Name");
addCaption(sheet, 0, 0, "URL");
addCaption(sheet, 1, 0, "Category");
//addLabel(1, 2, "a", times);


}
    

	private static void addCaption(WritableSheet sheet, int column, int row, String s)
			throws RowsExceededException, WriteException {
		Label label;
		label = new Label(column, row, s, timesBoldUnderline);
		sheet.addCell(label);
	}
    
    private static void createContent(String URL, String Category, int rownum) throws WriteException,
	RowsExceededException {
 Label label;
	label = new Label(0, rownum, URL, times);
	excelSheet.addCell(label);
	label = new Label(1, rownum, Category, times);
	excelSheet.addCell(label);
 
    //System.out.println("Added" + URL + "   " + Category);
}
    
    
    
    
}// class ends
    	
    	
    	
    	
    	
    